package cn.com.dhc.danlu.widget;

import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.renderscript.Int2;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.model.CategoryModel;
import cn.com.dhc.danlu.model.IChildable;
import cn.com.dhc.danlu.model.IItemable;
import cn.com.dhc.danlu.util.SerializeCache;

/**
 * @author wuhaiyang
 */
public class ListGridChooser<T extends  IChildable> extends LinearLayout{
    private View rootView;
    private TextView allTV;
    private LinearLayout llAllGoods;

    private boolean showAllBtn;
    private boolean showAll;

    private CategoryAdapter categoryAdapter;
    private ContentAdapter contentAdapter;

    private OnItemClickListener onItemClickListener;

    private OnShowAllClickListener onShowAllClickListener;

    private final int DEFAULT_POSITION = 0;

    private Resources res;

    private int categoryTextDefaultColor;
    private int categoryTextHighLightColor;
    private int allBtnTextDefaultColor;
    private int allBtnTextHighLightColor;
    private int contentTextDefaultColor;
    private int contentTextHighLightColor;

    public static final int ALL = -1;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnShowAllClickListener(OnShowAllClickListener onShowAllClickListener) {
        this.onShowAllClickListener = onShowAllClickListener;
    }

    public ListGridChooser(Context context) {
        super (context);
        initChooser (context);
    }

    public ListGridChooser(Context context, AttributeSet attrs) {
        super(context, attrs);
        initChooser (context);
    }

    public ListGridChooser(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initChooser (context);
    }

    public void setData(List<T> categoryRecords){
        categoryAdapter.setRecords(categoryRecords);

        if(categoryAdapter.getCount() > 0) {
            categoryAdapter.select(DEFAULT_POSITION);
        }
    }

    private void initChooser (Context context) {
        initResource ();
        final LayoutInflater inflater = LayoutInflater.from (context);
        rootView = inflater.inflate (R.layout.layout_list_grid_chooser, null);

        addView(rootView);

        final ListView listView = (ListView) rootView.findViewById (R.id.category_list);
        final GridView gridView = (GridView) rootView.findViewById (R.id.content_grid);

        allTV = (TextView) rootView.findViewById (R.id.tv_all);
        llAllGoods = (LinearLayout) rootView.findViewById(R.id.ll_all_goods);
        llAllGoods.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onShowAllClickListener != null) {
                    onShowAllClickListener.onShowAll(categoryAdapter.getSelectedItem(), categoryAdapter.getSelectedPosition(), ALL);
                }
                showAll = true;
                setAllBtnTextColor();

                contentAdapter.clearSelected();
            }
        });
        categoryAdapter = new CategoryAdapter(context, R.layout.item_text, new ArrayList<CategoryModel>());
        listView.setAdapter(categoryAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                categoryAdapter.setSelectedPosition(position);

                IChildable item = categoryAdapter.getItem(position);
                showAll = false;
                if(showAllBtn){
                    if(allTV.getVisibility() != VISIBLE){
                        allTV.setVisibility(View.VISIBLE);
                    }
                    allTV.setText(res.getString(R.string.all_x_goods, item.getName()));
                }else{
                    if(allTV.getVisibility() != GONE){
                        allTV.setVisibility(View.GONE);
                    }
                }
                setAllBtnTextColor();

                contentAdapter.setRecords(item.getChildrens());
            }
        });

        contentAdapter = new ContentAdapter(context, R.layout.item_text, new ArrayList<CategoryModel>());
        gridView.setAdapter(contentAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                contentAdapter.select(position);
                showAll = false;
                setAllBtnTextColor();
                SerializeCache.getInstance().saveCategoryIndexs(categoryAdapter.getSelectedPosition(), position);

                IItemable item = contentAdapter.getItem(position);
                if(onItemClickListener != null) {
                    onItemClickListener.onItemClick(item, categoryAdapter.getItem(categoryAdapter.getSelectedPosition()), categoryAdapter.getSelectedPosition(), position);
                }
            }
        });


    }

    public void toggle(){
        if(getVisibility() == VISIBLE){
            setVisibility(GONE);
        }else{
            setVisibility(VISIBLE);
        }
    }

    public void showAllBtn(){
        showAllBtn = true;
        allTV.setVisibility(View.VISIBLE);
    }

    public void hideAllBtn(){
        showAllBtn = false;
        allTV.setVisibility(View.GONE);
    }

    public void setAllBtnText(String str){
        allTV.setText(str);
    }

    public void setAllBtnText(int strResId){
        allTV.setText(strResId);
    }

    private void setAllBtnTextColor(){
        if(showAll) {
            allTV.setTextColor(allBtnTextHighLightColor);
        }else{
            allTV.setTextColor(allBtnTextDefaultColor);
        }
    }

    class CategoryAdapter<T extends  IChildable> extends ArrayAdapter{
        private LayoutInflater mInflater;
        private List<T> records;

        private int selectedPosition;

        public CategoryAdapter(Context context, int resource, List<T> records) {
            super(context, resource, records);
            this.mInflater = LayoutInflater.from(context);
            this.records = records == null ? new ArrayList<T>() : records;

        }

        @NonNull
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.item_text, parent,false);
                holder.rootLL = (LinearLayout) convertView.findViewById(R.id.ll_root);
                holder.textTV = (TextView) convertView.findViewById(R.id.tv_text);
                holder.textTV.setSingleLine();
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            if(selectedPosition == position){
                holder.textTV.setTextColor(categoryTextHighLightColor);
                holder.rootLL.setBackgroundResource(R.drawable.bg_white_right_red_bar);
            }else{
                holder.textTV.setTextColor(categoryTextDefaultColor);
                holder.rootLL.setBackgroundResource(R.drawable.bg_list_item);
            }
            holder.textTV.setText(records.get(position).getName());

            return convertView;
        }

        public void setSelectedPosition(int selectedPosition) {
            this.selectedPosition = selectedPosition;
            notifyDataSetChanged();
        }

        public int getSelectedPosition() {
            return selectedPosition;
        }

        public IChildable getSelectedItem(){
            return getItem(selectedPosition);
        }

        public void select(int selectedPosition){
            this.selectedPosition = selectedPosition;
            contentAdapter.setRecords(getItem(selectedPosition).getChildrens());
            if(showAllBtn){
                allTV.setText(res.getString(R.string.all_x_goods, getItem(selectedPosition).getName()));
            }
            notifyDataSetChanged();
        }

        @Nullable
        @Override
        public T getItem(int position) {
            return records.get(position);
        }

        @Override
        public int getCount() {
            return super.getCount();
        }

        public void setRecords(List<T> records) {
            this.records.clear();
            this.records.addAll(records);
            notifyDataSetChanged();
        }

        class ViewHolder {
            LinearLayout rootLL;
            TextView textTV;
        }
    }

    class ContentAdapter<T extends IItemable> extends ArrayAdapter{
        private LayoutInflater mInflater;
        private List<T> records;

        private int selectedPosition = -1;

        public ContentAdapter(Context context, int resource, List<T> records) {
            super(context, resource, records);
            this.mInflater = LayoutInflater.from(context);
            this.records = records == null ? new ArrayList<T>() : records;
        }

        @NonNull
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.item_icon_text, null);
                holder.textTV = (TextView) convertView.findViewById(R.id.tv_text);
                holder.iconIV = (SimpleDraweeView) convertView.findViewById(R.id.sdv_icon);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            final IItemable item = records.get(position);

            holder.textTV.setText(item.getText());
            String url = item.getIconURL();
            if (!TextUtils.isEmpty(url)) {
                holder.iconIV.setImageURI(Uri.parse(url));
            }else{
                holder.iconIV.setImageResource(R.drawable.ic_default);
            }

            if(position == selectedPosition){
                holder.textTV.setTextColor (contentTextHighLightColor);
            }else{
                holder.textTV.setTextColor (contentTextDefaultColor);
            }

            return convertView;
        }

        @Override
        public int getCount() {
            return super.getCount();
        }

        public void setRecords(List<T> records) {
            selectedPosition = -1;
            this.records.clear();
            this.records.addAll(records);
            notifyDataSetChanged();
        }

        public int getSelectedPosition() {
            return selectedPosition;
        }



        @Nullable
        @Override
        public T getItem(int position) {
            return records.get(position);
        }

        public void clearSelected(){
            selectedPosition = -1;
            notifyDataSetChanged();
        }

        public void select(int selectedPosition){
            this.selectedPosition = selectedPosition;
            notifyDataSetChanged();
        }

        class ViewHolder {
            TextView textTV;
            SimpleDraweeView iconIV;
        }
    }

    public Int2 getSelectedIndexs(){
        int categorySelectedIndex = categoryAdapter.getSelectedPosition();
        int contentSelectedIndex = contentAdapter.getSelectedPosition();
        Int2 selectedIndexs = new Int2(categorySelectedIndex, contentSelectedIndex);
        return  selectedIndexs;
    }

    public void setSelectedIndexs(int categorySelectedIndex, int contentSelectedIndex){
        if(categorySelectedIndex != -1){
            categoryAdapter.select(categorySelectedIndex);

            if(contentSelectedIndex == -1){
                contentAdapter.clearSelected();
                showAll = true;
            }else {
                contentAdapter.select(contentSelectedIndex);
                showAll = false;
            }
            setAllBtnTextColor();
        }
    }



    private void initResource(){
        this.res = getResources();

        this.allBtnTextDefaultColor = res.getColor(R.color.label_cg);
        this.allBtnTextHighLightColor = res.getColor(R.color.search_bg);
        this.categoryTextDefaultColor = res.getColor(R.color.label_cg);
        this.categoryTextHighLightColor = res.getColor(R.color.search_bg);
        this.contentTextDefaultColor = res.getColor(R.color.label_cg);
        this.contentTextHighLightColor = res.getColor(R.color.search_bg);
    }


    public interface OnItemClickListener {
        void onItemClick(IItemable item, IChildable parentItem, int categorySelectedIndex, int contentSelectedIndex);
    }

    public interface OnShowAllClickListener {
        void onShowAll(IChildable item, int categorySelectedIndex, int contentSelectedIndex);
    }

    public void setAllBtnTextDefaultColor(int allBtnTextDefaultColor) {
        this.allBtnTextDefaultColor = allBtnTextDefaultColor;
    }

    public void setAllBtnTextHighLightColor(int allBtnTextHighLightColor) {
        this.allBtnTextHighLightColor = allBtnTextHighLightColor;
    }

    public void setCategoryTextDefaultColor(int categoryTextDefaultColor) {
        this.categoryTextDefaultColor = categoryTextDefaultColor;
    }

    public void setCategoryTextHighLightColor(int categoryTextHighLightColor) {
        this.categoryTextHighLightColor = categoryTextHighLightColor;
    }

    public void setContentTextDefaultColor(int contentTextDefaultColor) {
        this.contentTextDefaultColor = contentTextDefaultColor;
    }

    public void setContentTextHighLightColor(int contentTextHighLightColor) {
        this.contentTextHighLightColor = contentTextHighLightColor;
    }
}